package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.wb.dao.WbScrapDao;
import com.cyt.work.wb.model.po.WbScrap;
import com.cyt.work.wb.model.vo.WbScrapVo;
import com.cyt.work.wb.service.WbScrapService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 维保模块-报废表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:28:18
 */
@Service("wbScrapService")
public class WbScrapServiceImpl extends BaseServiceImpl<WbScrap> implements WbScrapService {

    @Resource
    private WbScrapDao wbScrapDao;

    @Autowired
    public void setBaseDao(WbScrapDao wbScrapDao) {
        this.baseDao = wbScrapDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbScrapVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:18
     */
    @Override
    public List<WbScrap> listExample(WbScrapVo wbScrapVo) {
        Example example = new Example(WbScrap.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wbScrapDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbScrapVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:18
     */
    @Override
    public ReturnResult page(WbScrapVo wbScrapVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbScrap> list = listExample(wbScrapVo);
        PageInfo<WbScrap> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbScrapVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbScrapVo wbScrapVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbScrapVo.getId())) {
            WbScrap wbScrap = new WbScrap(sysUser);
            wbScrap.setApplyId(wbScrapVo.getApplyId());
            wbScrap.setDeviceId(wbScrapVo.getDeviceId());
            wbScrap.setDeviceName(wbScrapVo.getDeviceName());
            wbScrap.setDeviceNum(wbScrapVo.getDeviceNum());
            wbScrap.setDeviceModel(wbScrapVo.getDeviceModel());
            wbScrap.setScrapReason(wbScrapVo.getScrapReason());
            wbScrap.setDispose(wbScrapVo.getDispose());
            wbScrapDao.insert(wbScrap);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbScrap wbScrap = wbScrapDao.selectByPrimaryKey(wbScrapVo.getId());
            wbScrap.setApplyId(wbScrapVo.getApplyId());
            wbScrap.setDeviceId(wbScrapVo.getDeviceId());
            wbScrap.setDeviceName(wbScrapVo.getDeviceName());
            wbScrap.setDeviceNum(wbScrapVo.getDeviceNum());
            wbScrap.setDeviceModel(wbScrapVo.getDeviceModel());
            wbScrap.setScrapReason(wbScrapVo.getScrapReason());
            wbScrap.setDispose(wbScrapVo.getDispose());
            wbScrap.setUpdateTime(new Date());
            wbScrap.setUpdateUser(sysUser.getId());
            wbScrapDao.updateByPrimaryKey(wbScrap);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbScrapVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbScrapVo wbScrapVo, SysUser sysUser) {
        //要修改的字段
        WbScrap wbScrap = new WbScrap();
        wbScrap.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbScrap.setUpdateUser(sysUser.getId());
        wbScrap.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbScrap.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbScrapVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbScrapVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbScrapDao.updateByExampleSelective(wbScrap, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbScrapVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbScrapVo wbScrapVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbScrap.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbScrapVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbScrapVo.getId().split(",")));
        }
        wbScrapDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}